import { TimeStamp } from "../common/common";

export interface LookupListItem {
    modelName: string;
    from: string;
    localField: string;
    as?: string;
    foreignField?: string;
}

export interface ParentFind {
    parentModelName: string; // 父表模型名,如DictionaryModel
    localField: string; // 父表关联字段名,如dictItems
}

// 代码生成主表
export interface CodeGenerate extends TimeStamp {
    _id: string;
    modelName: string;
    pageName?: string;
    collectionName?: string;
    desc?: string;
    fields: string[] | CodeGenerateField[]; // 与CodeGenerateField[]关联
    operations: ("create" | "getPageList" | "getDetail" | "parentFind" | "update" | "delete")[];
    resultTypes: ("admin" | "client")[];
    // 根据父_id查询子数据列表的配置，例子：getDictionaryItems方法
    parentFind?: ParentFind;
    // 关联查询配置
    lookupList?: LookupListItem[];
    permissionKey?: string;
}

// 代码生成字段子表
export interface CodeGenerateField extends TimeStamp {
    _id: string;
    name: string;
    label?: string;
    type: "Number" | "String" | "Boolean" | "ObjectId" | "Object" | "ObjectIdArray" | "NumberArray" | "StringArray" | "BooleanArray" | "ObjectArray";
    componentType: "textarea" | "inputNumber" | "radio" | "select" | "input" | "autoComplete";
    isTable: boolean; // table是否显示该字段、是否查询该字段
    isForm: boolean; // 新增、修改表单是否显示改字段
    queryType?: "fuzzy" | "precise"; // 查询方式：模糊查询、精确查询，字段不存在时不根据该字段查询
    required: boolean;
    unique: boolean;
    sortType?: "asc" | "desc";
    dictType?: string;
}

export const FieldTypeInterfaceMap = {
    Number: "number",
    String: "string",
    Boolean: "boolean",
    ObjectId: "ObjectId",
    Object: "Record<string, any>",
    NumberArray: "number[]",
    StringArray: "string[]",
    BooleanArray: "boolean[]",
    ObjectIdArray: "string[]",
    ObjectArray: "Record<string, any>[]",
};

export const FieldTypeSchemaTypeMap = {
    Number: "Number",
    String: "String",
    Boolean: "Boolean",
    ObjectId: "Types.ObjectId",
    Object: "Object",
    NumberArray: "[Number]",
    StringArray: "[String]",
    BooleanArray: "[Boolean]",
    ObjectIdArray: "[Types.ObjectId]",
    ObjectArray: "[Object]",
};
